package com.huluobo.high3videoproject.setting.view;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.base.view.BaseActivity;
import com.huluobo.base.view.Event;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.bean.UpdateBeanResult;
import com.huluobo.high3videoproject.bean.UserInfo;
import com.huluobo.high3videoproject.mvp.login.view.LoginActivity;
import com.huluobo.high3videoproject.net.Constant;
import com.huluobo.high3videoproject.setting.presenter.SettingContract;
import com.huluobo.high3videoproject.setting.presenter.SettingPresenter;
import com.huluobo.high3videoproject.utils.DBUtils;
import com.huluobo.high3videoproject.utils.GlideUtils;
import com.hyphenate.chat.EMClient;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import org.greenrobot.eventbus.EventBus;

public class SettingActivity extends BaseActivity<SettingContract.View, SettingPresenter> implements SettingContract.View, View.OnClickListener {

    private ImageView setting_iv_head_icon;
    private TextView setting_tv_edit;

    private EditText setting_et_nick_name, setting_et_address, setting_et_birth;
    private Button setting_btn_nick_name, setting_btn_address, setting_btn_birth, setting_btn_logout;

    @Override
    public void onUpdateAddressSuccess(UpdateBeanResult updateBeanResult) {
        ToastUtils.showLong("修改地址成功");
        UserInfo userInfo = DBUtils.getInstance().queryUserInfo(SPUtils.getInstance().getString(Constant.SP_USERNAME));
        String address = setting_et_address.getText().toString();
        userInfo.setAddress(address);
        DBUtils.getInstance().updateUserInfo(userInfo);
    }

    @Override
    public void onUpdateAddressFailed(String error) {
        ToastUtils.showLong("修改地址失败:" + error);
    }

    @Override
    public void onUpdateBirthSuccess(UpdateBeanResult updateBeanResult) {
        ToastUtils.showLong("修改生日成功");
        UserInfo userInfo = DBUtils.getInstance().queryUserInfo(SPUtils.getInstance().getString(Constant.SP_USERNAME));
        String birth = setting_et_birth.getText().toString();
        userInfo.setBirth(birth);
        DBUtils.getInstance().updateUserInfo(userInfo);
    }

    @Override
    public void onUpdateBirthFailed(String error) {
        ToastUtils.showLong("修改生日失败:" + error);
    }

    @Override
    public void onUpdateIconSuccess(UpdateBeanResult updateBeanResult) {
        UserInfo userInfo = DBUtils.getInstance().queryUserInfo(SPUtils.getInstance().getString(Constant.SP_USERNAME));
        userInfo.setIcon(updateBeanResult.getResult());
        DBUtils.getInstance().updateUserInfo(userInfo);
        GlideUtils.loadNormal(this, setting_iv_head_icon, updateBeanResult.getResult());
        EventBus.getDefault().post(new Event("MineFragment", null, 103));
    }

    @Override
    public void onUpdateIconFailed(String error) {
        Log.i("TagA", "error:" + error);
    }

    @Override
    public void onUpdateNickNameSuccess(UpdateBeanResult updateBeanResult) {
        ToastUtils.showLong("修改昵称成功");
        UserInfo userInfo = DBUtils.getInstance().queryUserInfo(SPUtils.getInstance().getString(Constant.SP_USERNAME));
        String nickName = setting_et_nick_name.getText().toString();
        userInfo.setNickname(nickName);
        DBUtils.getInstance().updateUserInfo(userInfo);
        EventBus.getDefault().post(new Event("MineFragment", null, 104));
    }

    @Override
    public void onUpdateNickNameFailed(String error) {
        ToastUtils.showLong("修改昵称失败:" + error);
    }

    @Override
    protected void initData() {
        presenter = new SettingPresenter();
        UserInfo userInfo = DBUtils.getInstance().queryUserInfo(SPUtils.getInstance().getString(Constant.SP_USERNAME));
        GlideUtils.loadNormal(this, setting_iv_head_icon, userInfo.getIcon());

        setting_et_nick_name.setText(userInfo.getNickname());
        setting_et_address.setText(userInfo.getAddress());
        setting_et_birth.setText(userInfo.getBirth());
    }

    @Override
    protected void initView() {
        setting_iv_head_icon = findViewById(R.id.setting_iv_head_icon);
        setting_tv_edit = findViewById(R.id.setting_tv_edit);
        setting_et_nick_name = findViewById(R.id.setting_et_nick_name);
        setting_et_address = findViewById(R.id.setting_et_address);
        setting_et_birth = findViewById(R.id.setting_et_birth);
        setting_btn_nick_name = findViewById(R.id.setting_btn_nick_name);
        setting_btn_address = findViewById(R.id.setting_btn_address);
        setting_btn_birth = findViewById(R.id.setting_btn_birth);
        setting_btn_logout = findViewById(R.id.setting_btn_logout);

        //设置点击事件
        setting_iv_head_icon.setOnClickListener(this);
        setting_tv_edit.setOnClickListener(this);
        setting_btn_nick_name.setOnClickListener(this);
        setting_btn_address.setOnClickListener(this);
        setting_btn_birth.setOnClickListener(this);
        setting_btn_logout.setOnClickListener(this);
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_iv_head_icon:
            case R.id.setting_tv_edit:
                //设置图片选择事件
                uploadHeadIcon();
                break;
            case R.id.setting_btn_nick_name:
                //修改昵称
                uploadNickname();
                break;
            case R.id.setting_btn_address:
                //修改地址
                uploadAddress();
                break;
            case R.id.setting_btn_birth:
                //修改生日
                uploadBirth();
                break;
            case R.id.setting_btn_logout:
                //退出登录
                logout();
                break;
            default:
                break;
        }
    }

    private void logout() {
        EMClient.getInstance().logout(true);
        //清理SP中所有内容,包括Token,username,userid
        SPUtils.getInstance().clear();
        EventBus.getDefault().post(new Event("MainActivity", null, 105));
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void uploadBirth() {
        String birth = setting_et_birth.getText().toString();
        if (TextUtils.isEmpty(birth)) {
            ToastUtils.showLong("请输入生日");
            return;
        }
        presenter.updateBirth(birth);
    }

    //修改地址
    private void uploadAddress() {
        String address = setting_et_address.getText().toString();
        if (TextUtils.isEmpty(address)) {
            ToastUtils.showLong("请输入地址");
            return;
        }
        presenter.updateAddress(address);
    }

    //修改昵称
    private void uploadNickname() {
        String nickName = setting_et_nick_name.getText().toString();
        if (TextUtils.isEmpty(nickName)) {
            ToastUtils.showLong("请输入昵称");
            return;
        }
        presenter.updateNickName(nickName);
    }

    /**
     * 选择图片上传
     */
    private void uploadHeadIcon() {
        PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture();
    }

    /**
     * 选择图片的回调
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            PictureBean bean = bundle.getParcelable(PictureSelector.PICTURE_RESULT);
            String path = bean.getPath();
            if (!TextUtils.isEmpty(path)) {
                presenter.updateIcon(path);
            }
        }
    }
}